<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar
      left-arrow
      @click-left="onClickLeft"
      title="编辑资料"
    ></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell is-link title="名称" @click="showName" :value="userInfo.name" />
      <van-cell
        is-link
        title="性别"
        @click="showSex = true"
        :value="userInfo.gender === 0 ? '男' : '女'"
      />
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="changeBirthday"
      />
    </van-cell-group>
    <!-- 编辑姓名 -->
    <van-dialog
      @confirm="saveName"
      v-model="nameShow"
      title="编辑名称"
      show-cancel-button
    >
      <van-field ref="nameDialog" v-model="name" type="text" />
    </van-dialog>
    <!-- 编辑性别 -->
    <van-popup v-model="showSex" position="bottom">
      <!-- 顶部导航栏 -->
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showSex = false"
      />
      <!-- 性别选择 -->
      <van-cell-group>
        <van-cell title="男" is-link @click="changeSex(0)" />
        <van-cell title="女" is-link @click="changeSex(1)" />
      </van-cell-group>
    </van-popup>
    <!-- 日期选择 -->
    <van-popup v-model="dateShow" position="bottom">
      <van-datetime-picker
        v-model="birthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="dateShow = false"
        @confirm="saveBirthday"
      />
    </van-popup>
    <!-- 剪裁图片 -->
    <div class="current-img" v-show="isShowCropper">
      <vueCropper
        ref="cropper"
        fixed
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button @click="submitPhoto" class="confirm-btn" type="primary"
        >确认</van-button
      >
      <van-button
        @click="isShowCropper = false"
        class="cancel-btn"
        type="primary"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入剪裁图片插件
import { VueCropper } from 'vue-cropper'
// 导入修改时间组件
import moment from 'moment'
// 导入接口
import { editUserInfo, uploaderPhoto } from '../../api/edit'
// import { nextTick } from 'vue/types/umd'
// 导入辅助计算机自动计算属性
import { mapState } from 'vuex'
// import { Toast } from 'vant'
// import { DatetimePicker } from 'vant'
export default {
  name: 'edit',
  data () {
    return {
      nameShow: false, // 显示名字
      name: '',
      showSex: false, // 显示性别
      dateShow: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      birthday: new Date(2011, 0, 0),
      img: '',
      isShowCropper: false
    }
  },
  components: {
    VueCropper
  },
  methods: {
    // 裁切完  上传图片
    submitPhoto () {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(blob => {
        const data = new FormData()
        data.append('photo', blob)
        uploaderPhoto(data).then(res => {
          console.log(res)
          this.$toast.success('更新成功')
          this.$store.commit('getUserInfos', {
            ...this.userInfo,
            // 使用最新的name覆盖原始的name
            photo: res.data.data.photo
          })
          this.isShowCropper = false
        })
        console.log(data)
      })
    },
    // 头像上传
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      console.log(file)
      // 展示裁切图片
      this.isShowCropper = true
      // 设置裁切图片  base64编码
      this.img = file.content
      // 实例化formDate
      // const data = new FormData()
      // data.append('photo', file.file)
      // uploaderPhoto(data).then(res => {
      //   console.log(res)
      //   this.$toast.success('更新成功')
      //   this.$store.commit('getUserInfos', {
      //     ...this.userInfo,
      //     // 使用最新的name覆盖原始的name
      //     photo: res.data.data.photo
      // })
      // })
    },
    // 点击确定修改生日
    saveBirthday () {
      // console.log(this.birthday)
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        // console.log(res)
        this.$toast.success('更新成功')
        this.dateShow = false
        this.$store.commit('getUserInfos', {
          ...this.userInfo,
          // 使用最新的name覆盖原始的name
          birthday: res.data.data.birthday
        })
      })
    },
    // 修改生日
    changeBirthday () {
      this.dateShow = true
      this.birthday = new Date(this.userInfo.birthday)
    },
    // 修改性别
    changeSex (sex) {
      editUserInfo({ gender: sex }).then(res => {
        // console.log(res)
        this.$toast.success('更新成功')
        this.$store.commit('getUserInfos', {
          ...this.userInfo,
          // 使用最新的name覆盖原始的name
          gender: sex
        })
      })
      this.showSex = false
    },
    // 点击保存昵称
    saveName () {
      editUserInfo({ name: this.name }).then(res => {
        this.$toast.success('更新成功')
        this.$store.commit('getUserInfos', {
          ...this.userInfo,
          // 使用最新的name覆盖原始的name
          name: res.data.data.name
        })
        // console.log(res)
      })
    },
    // 编辑框事件
    showName () {
      this.nameShow = true
      this.name = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.nameDialog.focus()
      })
    },
    onClickLeft () {
      this.$router.push({ path: '/my' })
    }
  },
  computed: mapState(['userInfo'])
  // computed: {
  //   // 简化代码
  //   userInfo () {
  //     return this.$store.state.userInfo
  //   }
  // }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  // .van-nav-bar {
  //   background-color: #3196fa;
  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  //   .van-icon-arrow-left {
  //     color: #fff;
  //   }
  // }
  // 头像部分
  .avatar {
    position: relative;
    padding: 20px 0;
    text-align: center;
    background-image: url(../../assets/yu.jpg);
    background-size: 100% 100%;
    .van-image {
      width: 120px;
      height: 120px;
      opacity: 0.7;
    }
    .van-uploader {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      height: 120px;
      width: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
}
.van-dialog__content {
  margin: 10px 0;
  padding: 0 15px;
}
.current-img {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .confirm-btn {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .cancel-btn {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
